<?php

	include_once(dirname(__FILE__).'/Weixin.php');
 	//include_once(dirname(__FILE__).'/../includes/conn.php');
 	$encodingAesKey = 'XTO5wvlECDgkUsMd5wjjFFb1ZyU6XdgUZA4GJMs7yqP';
    $token = 'dokao';
    $appId = 'wx73e6cc5c05f2f253';
    // logResult('/pp.txt','kkkk');
	$timeStamp  = empty($_GET['timestamp'])     ? ""    : trim($_GET['timestamp']) ;
	$nonce      = empty($_GET['nonce'])     ? ""    : trim($_GET['nonce']) ;
	$msg_sign   = empty($_GET['msg_signature']) ? ""    : trim($_GET['msg_signature']) ;
	$encryptMsg = file_get_contents('php://input');

	$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
 
	$xml_tree = new DOMDocument();
	$xml_tree->loadXML($encryptMsg);
	$array_e = $xml_tree->getElementsByTagName('Encrypt');
	$encrypt = $array_e->item(0)->nodeValue;


	$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
	$from_xml = sprintf($format, $encrypt);

	$msg = '';
	$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
	// file_put_contents('./pp2.txt', $errCode);
	//var_dump($errCode);die;
	if ($errCode == 0) {
	    
	    $xml = new DOMDocument();
	    $xml->loadXML($msg);
	    $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
	    $component_verify_ticket = $array_e->item(0)->nodeValue;
	   if(!empty($component_verify_ticket)){
	    	$con = mysqli_connect("127.0.0.1","root","6195421","shop");
	    	
	    	mysqli_query($con,"UPDATE aicece_auth_account SET `ComponentVerifyTicket`='".substr($component_verify_ticket,9)."', `push_date`='".date('Y-m-d H:i:s')."' where `com_appid`='".$appId."'");
	    	mysqli_close($con);
	    }
	   
	    echo 'success';

	} else {
	    //logResult('/start.txt','解密后失败：'.$errCode);
	    print($errCode . "\n");
	}
	
	die();

?>
